<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Log;

/**
 * Class VoteWorksClick
 *
 * @package App\Models
 * @property int $id
 * @property string $created_at
 * @property string $updated_at
 * @property int $vote_id
 * @property int $works_id
 * @property int $rater_id
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteWorksClick whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteWorksClick whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteWorksClick whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteWorksClick whereVoteId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteWorksClick whereWorksId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteWorksClick whereRaterId($value)
 * @mixin \Eloquent
 */
class VoteWorksClick extends Model
{
    protected $fillable = ['vote_id', 'works_id', 'rater_id'];

    /**
     * 保存作品查看记录
     * @param $voteId
     * @param $worksId
     * @param $raterId
     * @return bool
     */
    public static function viewWorks($voteId, $worksId, $raterId)
    {
        $exists = self::where('vote_id', $voteId)
            ->where('works_id', $worksId)
            ->where('rater_id', $raterId)
            ->exists();
        if ($exists) {
            return true;
        } else {
            try {
                self::create([
                    'vote_id'  => $voteId,
                    'works_id' => $worksId,
                    'rater_id' => $raterId
                ]);
                return true;
            } catch (\Exception $e) {
                Log::error('保存作品查看记录失败' . $e->getMessage());
                return false;
            }
        }
    }
}